<template>
  <div class="container">
    <el-card class="card-style">
      <el-form ref="form" :model="form" :inline="true">
        <el-row :gutter="20">
          <el-col :span="21"
            ><el-form-item label="关键字" style="margin-left: 20px">
              <el-input
                v-model="form.keyword"
                style="width: 195px"
                placeholder="根据编号搜索"
                size="small"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-button
                size="small"
                style="margin-left: 10px"
                @click.native="cleanText"
                >清除</el-button
              >
              <el-button
                type="primary"
                size="small"
                @click.native="searchRandoms"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 15px"
      ></el-alert>
      <el-table :data="randomsList" style="width: 100%" v-loading="loading">
        <el-table-column label="编号" width="220" prop="id"> </el-table-column>
        <el-table-column prop="questionType" label="题型">
          <template #default="{ row }">
            {{ changequestionType(row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="220">
          <template #default="{ row }">
            <div
              class="numberbox"
              v-for="(item, index) in row.questionIDs"
              :key="index"
            >
              <a
                @click.prevent="Preview(item.id)"
                href="#"
                style="color: rgb(0, 153, 255)"
                >{{ item.number }}</a
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="180">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)" width="167">
          <!-- <template #default="{ row }">
            {{ row }}
          </template> -->
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)" width="165">
          <!-- <template #default="{ row }">
            {{ row }}
          </template> -->
        </el-table-column>
        <el-table-column label="录入人" prop="userName" width="165">
          <!-- <template #default="{ row }">
            {{ getcreatorID(row.userNmae) }}
          </template> -->
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="{ row }">
            <el-button
              @click.native="delData(row.id)"
              type="danger"
              icon="el-icon-delete"
              class="
                el-button el-button--danger el-button--small
                is-plain is-circle
              "
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagFather">
        <el-pagination
          class="paging"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="pagesize"
          layout="  prev, pager, next,sizes, jumper"
          :total="counts"
          >>
        </el-pagination>
      </div>
    </el-card>
    <questionsPreview
      :dialogPerview.sync="dialogPerview"
      :preview="preview"
      :videoURL="videoURL"
    ></questionsPreview>
  </div>
</template>

<script>
import questionsPreview from '../components/questions-preview.vue'
import { questionType } from '@/api/hmmm/constants.js'
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
export default {
  components: {
    questionsPreview
  },
  data () {
    return {
      form: {
        keyword: '' // 关键字
      }, // 上表单数据
      randomsList: [], // 表格数据
      loading: false, // loading效果
      page: 1, // 当前页
      pagesize: 20, // 页数量
      counts: 0, // 总条数
      dialogPerview: false, // 控制弹层打开关闭
      preview: {}, // 预览需要的数据
      videoURL: '' // 视频地址
    }
  },
  created () {
    this.getradomList()
    // this.getcreatorID()
  },
  methods: {
    // 获取页面数据
    async getradomList () {
      this.loading = true
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.form.keyword
      })
      // console.log(data)
      this.counts = data.counts
      this.randomsList = data.items
      this.loading = false
    },
    // 点击搜索方法
    searchRandoms () {
      this.page = 1
      this.getradomList()
    },
    // 点击清除的方法
    cleanText () {
      this.form.keyword = ''
    },
    // 点击删除的方法
    delData (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '温馨提示', {
        type: 'warning'
      })
        .then(async () => {
          await removeRandoms({ id })
          this.$message.success('删除成功')
          if (this.randomsList.length <= 1 && this.page > 1) {
            this.page--
          }
          this.getradomList()
        })
        .catch(() => {})
    },
    // 转换数据方法
    changequestionType (val) {
      const res = questionType.find((item) => item.value === +val)
      return res.label
    },
    // 切换每页多少条执行的方法
    handleSizeChange (val) {
      // console.log(val)
      this.pagesize = val
      this.page = 1
      this.getradomList()
    },
    // 切换当前页执行的方法
    handleCurrentChange (val) {
      // console.log(val)
      this.page = val
      // this.search()
      this.getradomList()
    },
    // 点击ID预览的效果
    async Preview (id) {
      const { data } = await detail({ id })
      this.preview = data
      this.videoURL = data.videoURL
      this.dialogPerview = true
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  ::v-deep .el-table th {
    background-color: #fafafa;
    border-bottom: 2px solid #e8e8e8 !important;
  }
  .card-style {
    margin: 10px 10px 0;
  }
  .pagFather {
    margin-top: 21px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
